<?php

/**
 * This is the model class for table "{{adzone}}".
 *
 * The followings are the available columns in table '{{adzone}}':
 * @property integer $zdz_id
 * @property string $zdz_name
 * @property string $zdz_assist_id
 * @property string $zmg_id
 * @property integer $zdz_type
 * @property integer $zna_id
 * @property integer $zdz_style
 * @property string $zdz_desc
 * @property double $zdz_price
 * @property integer $zdz_width
 * @property integer $zdz_height
 * @property integer $zdz_max_ads
 * @property integer $inputtime
 * @property integer $updatetime
 */
class Adzone extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Adzone the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{adzone}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zdz_name, zna_id, inputtime, updatetime', 'required'),
			array('zdz_type, zdz_associate, zna_id, zdz_style, zdz_width, zdz_height, zdz_max_ads, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zdz_price', 'numerical'),
			array('zdz_name, zdz_assist_id', 'length', 'max'=>100),
			array('zmg_id', 'length', 'max'=>255),
			array('zdz_desc', 'safe'),
			array('zdz_assist_id', 'unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zdz_id, zdz_name, zdz_assist_id, zmg_id, zdz_type, zna_id, zdz_style, zdz_desc, zdz_price, zdz_width, zdz_height, zdz_max_ads, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'adAmount'=>array(self::STAT,'Adse','zdz_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zdz_id' => 'ID',
			'zdz_name' => '广告位名称',
			'zdz_assist_id' => '广告位标识',
			'zmg_id' => '可见会员组',
			'zdz_type' => '广告类型',
			'zdz_associate' => '关联分类类型',
			'zna_id' => '所在页面',
			'zdz_style' => '样式',
			'zdz_desc' => '描述',
			'zdz_price' => '广告价格',
			'zdz_width' => '广告位宽度',
			'zdz_height' => '广告位高度',
			'zdz_max_ads' => '广告显示数量',
			'inputtime' => '创建时间',
			'updatetime' => '修改时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zdz_id',$this->zdz_id);
		$criteria->compare('zdz_name',$this->zdz_name,true);
		$criteria->compare('zdz_assist_id',$this->zdz_assist_id,true);
		$criteria->compare('zmg_id',$this->zmg_id,true);
		$criteria->compare('zdz_type',$this->zdz_type);
		$criteria->compare('zna_id',$this->zna_id);
		$criteria->compare('zdz_style',$this->zdz_style);
		$criteria->compare('zdz_desc',$this->zdz_desc,true);
		$criteria->compare('zdz_price',$this->zdz_price);
		$criteria->compare('zdz_width',$this->zdz_width);
		$criteria->compare('zdz_height',$this->zdz_height);
		$criteria->compare('zdz_max_ads',$this->zdz_max_ads);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);
		$criteria->order = 'zna_id asc';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	/*
	 * 根据菜单id获取所有广告位
	* */
	public function getAdzones($znaid){
		$criteria=new CDbCriteria;
		$criteria->compare('zna_id',$znaid);
		$adzones = $this->findAll($criteria);
		return $adzones;
	}
	/*
	 * 根据菜单id获取所有广告位作为菜单数据
	* */
	public function getAdzonesForSelect($znaid){
		$adzones = $this->getAdzones($znaid);
		$adzones = $adzones ? simplifyData($adzones) : array();
		$dataArray[0] = '请选择';
		foreach ($adzones as $arr){
			$dataArray[$arr['zdz_id']] = $arr['zdz_name'];
		}
		return $dataArray;
	}
    
	/**
	 * 批量操作
	 * $idbox id数组
	 * $memberid 会员id
	 * */
	public function batchChange($idbox){
		$condition = 'zdz_id in('.implode(',', $idbox).')';
		return $this->deleteAll($condition);
	}
    
	/**
	 * 调用广告位数据
	 * 
	 * */
	public function getData($id,$cid=0){
		$adzone = $this->findByPk($id)->attributes;
		$memgroup = Yii::app()->user->getState('memgroup');
		$time   = time();
		$criteria = new CDbCriteria();
		//判断广告位是否关联会员组，及当前会员的会员组是否可见
		if($adzone['zmg_id'] && (!$memgroup || $memgroup && in_array($memgroup['group']['zmg_id'], explode(',',$adzone['zmg_id']) ))){
			$adzone['ads'] = array();
		}else{
			if($cid){//关联分类
				$criteria->addCondition(new CDbExpression('FIND_IN_SET('.$cid.',zds_category)'));
			}
			$criteria->compare('zdz_id', $id);
			$criteria->compare('zds_start_time', '<'.$time);
			$criteria->compare('zds_end_time', '>'.$time);
			$criteria->compare('zdz_id', $id);
			$criteria->limit = $adzone['zdz_max_ads'];
			$criteria->order = new CDbExpression('RAND()');
			$ads    = Adse::model()->findAll($criteria);
			if(!empty($ads)){
				$ads = simplifyData($ads);
			}
			$adzone['ads'] = $ads;
		}		
		return $adzone;		
	}

	
	
	
	
	
	
}